package Classes.GameViews.ViewPanels
{
	/**
	 *获得物品显示面板 
	 */	
	import Classes.GameDatas.GD_Mission;
	import Classes.GameDatas.GD_Player;
	import Classes.GameUtils.GD_deal;
	import Classes.GameUtils.GU_CopyDisplay;
	import Classes.GameUtils.GU_filters;
	import Classes.GameViews.GV_Map;
	import Classes.GameViews.ViewIcons.ViewIcon;
	import Classes.GameViews.ViewScenes.VS_Main;
	
	import UIComponents.IMission;
	import UIComponents.IUseItem;
	
	import com.greensock.TweenLite;
	
	import flash.display.DisplayObject;
	import flash.display.Sprite;
	import flash.events.Event;
	import flash.events.TimerEvent;
	import flash.geom.Point;
	import flash.system.ApplicationDomain;
	import flash.text.TextField;
	import flash.utils.Timer;
	
	public class VP_getItems extends ViewPanel
	{
		static private var _instance:VP_getItems;
		private var _interfaceUI:Sprite;
		public var showX:Number;
		public var showY:Number;
		public var hideX:Number;
		public var hideY:Number;
		
		public var scaleV:Number=.7;
		public var ipWidth:Number=60;
		public var ipHight:Number=80;
		
		public function VP_getItems()
		{	
			init();
		}
		
		public function init():void
		{
			_interfaceUI		=	new  (ApplicationDomain.currentDomain.getDefinition("PanelGetItems_v") as Class);
			this._interfaceUI.scaleX	=	scaleV;
			this._interfaceUI.scaleY	=	scaleV;
			this.addChild(_interfaceUI);
			this.openAt(VS_Main.instance);
			
			upDatePosition();
			( (this.interfaceUI.getChildByName("item1") as Sprite).getChildByName("collectNum") as Sprite).visible	=	false;
			( (this.interfaceUI.getChildByName("item2") as Sprite).getChildByName("collectNum") as Sprite).visible	=	false;
			( (this.interfaceUI.getChildByName("item3") as Sprite).getChildByName("collectNum") as Sprite).visible	=	false;
			( (this.interfaceUI.getChildByName("item4") as Sprite).getChildByName("collectNum") as Sprite).visible	=	false;
			( (this.interfaceUI.getChildByName("item5") as Sprite).getChildByName("collectNum") as Sprite).visible	=	false;
			( (this.interfaceUI.getChildByName("item1" ) as Sprite).getChildByName("bj") as Sprite).visible	=	false;
			( (this.interfaceUI.getChildByName("item2" ) as Sprite).getChildByName("bj") as Sprite).visible	=	false;
			( (this.interfaceUI.getChildByName("item3" ) as Sprite).getChildByName("bj") as Sprite).visible	=	false;
			( (this.interfaceUI.getChildByName("item4" ) as Sprite).getChildByName("bj") as Sprite).visible	=	false;
			( (this.interfaceUI.getChildByName("item5" ) as Sprite).getChildByName("bj") as Sprite).visible	=	false;
			
			addListener();
		}
		
		public function addListener():void
		{
			
		}
		
		//**********************************************************************显示获得物品*************************
		/**
		 *显示获得的物品
		 * @param item
		 */
		public function showGetItem(itemId:Number):void
		{	
			//			trace(GD_deal.instance.getStr(itemId));
			if(GD_deal.instance.getStr(itemId)!=130)return;
			var im:IMission		= 	getMissionByItem(itemId);
		}
		
		/**
		 *跟据物品，返回任务 
		 * @param item
		 * @return 
		 */
		public function getMissionByItem(itemId:Number):IMission
		{
			this.cleanGetAry();
			
			var s:Array	=GD_Player.instance.collectMissionArr();
			var len:int=s.length;
			for(var i:int=0; i < len; i++)
			{
				var gd:GD_Mission=s[i] as GD_Mission;
				
				var ob:Object;
				for each( ob 	in	gd.conditionCountsAry){
					//					{countType:count.countType,description:count.description,nowValue:count.nowValue,targetValue:count.targetValue,needItemId:count.needItemId,countSecondType:count.countThirdType}
					if(ob.needItemId==itemId){
						//						return gd;
						var item:IUseItem;
						var iob:Object;
						for each( iob in gd.conditionCountsAry){
							if(iob.needItemId!=null){
								item	=	GD_deal.instance.getHaveItemById(iob.needItemId);
								if(item==null)item = GD_deal.instance.getDataById(iob.needItemId);
								//									item = GD_deal.instance.getDataById(iob.needItemId);
								var vi:ViewIcon		=	item.itemPic as ViewIcon;
								var newVI:ViewIcon;
								newVI	=	new ViewIcon(vi.sourceName, vi.picName, this.ipWidth, this.ipHight);
								newVI.numSP["num"]	=	vi.numSP["num"];
								if( Number( vi.numSP["num"]) < 1) newVI.filters	=	[GU_filters.grayfilter];
								
//								var ip:DisplayObject	=	GU_CopyDisplay.duplicateDisplayObject(item.itemPic,item);
//								ip.width	=	this.ipWidth;
//								ip.height	=	this.ipHight;
								showGetPicAry.push(newVI);
							}
						}	
						
						showGetPicAry.push(gd.rewardCountsAry[0]);
						setGetPicPosition();
//						this.openPanel();
						return gd;
					}
					//					trace(ob);
				}
			}
			return null;
		}
		
		//设获得的物品置位
		public var showGetPicAry:Array=new Array;
		public function setGetPicPosition():void{
			var picWidth:uint	=	40;
			var pmPoint:Point	=	(VP_Main.instance.interfaceUI.getChildByName("bar_system") as DisplayObject).localToGlobal(new Point(0,0));//new Point(GV_Map.instance.stage.stageWidth,GV_Map.instance.stage.stageHeight/2);
			var offsetY:Number	=	pmPoint.y+200;
			
//			var offsetX:Number		= 	pmPoint.x - (showGetPicAry.length * picWidth)-picWidth;
			
			for(var i:uint=0; i<5; i++)
			{
				var pic:Sprite	= ( (this.interfaceUI.getChildByName("item"+(i+1) ) as Sprite).getChildByName("pc") as Sprite);
				while(pic.numChildren>0)pic.removeChildAt(0);
				
				if(i<showGetPicAry.length)
				{
//					( (this.interfaceUI.getChildByName("item"+(i+1) ) as Sprite).getChildByName("bj") as Sprite).visible	=	false;
					pic.addChild(showGetPicAry[i]);
//					pic.width	=	this.ipWidth;
//					pic.height	=	this.ipHight;
				}else{
//					( (this.interfaceUI.getChildByName("item"+(i+1) ) as Sprite).getChildByName("bj") as Sprite).visible	=	true;
				}
					
//				showGetPicAry[i].x	= 	offsetX;
//				showGetPicAry[i].y	=	offsetY;
//				(showGetPicAry[i] as Sprite).width	   =	40;
//				(showGetPicAry[i] as Sprite).height    =	40;
//				GV_Map.instance.stage.addChild(showGetPicAry[i]);
				
			}
			
			this.toShow();
			
			var t:Timer	=new Timer(3000,1);
			t.addEventListener(TimerEvent.TIMER,function(e:Event):void{
				toHide();
			});
			t.start();
			
			
		}
		
		/**********************************************************************    panel handler ***********************/
		public function upDatePosition():void
		{
			showX	=	GV_Map.instance.stage.stageWidth-this.interfaceUI.width;
			hideX	=	GV_Map.instance.stage.stageWidth;
			showY	=	hideY	=	120;
			
			this.x 	=	this.hideX;
			this.y	=	this.showY;
		}
		
		
		/**
		 *清空可显示对像 
		 * 
		 */
		public function cleanGetAry():void
		{
			while(showGetPicAry.length){
				(showGetPicAry.shift() as ViewIcon).release();
			}
		}
		
		/**
		 *打开面板 
		 * 
		 */
		public function openPanel():void
		{
			this.openAt(VS_Main.instance,true);
//			this.toCenter();
		}
		
		public function toHide():void
		{
//			this.x		=	this.stage.stageWidth;
			TweenLite.to(this, 0.5, {x:hideX,y:hideY}); 
		}
		public function toShow():void
		{
			TweenLite.to(this, 0.5, {x:showX,y:showY});
		}
		/**
		 *关闭面板 
		 * @param e
		 * 
		 */
		public function closeHandler(e:Event):void
		{
			this.close();
		}
		
		/**
		 *设置数据 
		 * @param shops
		 * 
		 */
		public function setData(shops:Array):void
		{
		}
		
		
		
		public static function get instance():VP_getItems
		{
			if(!_instance)_instance=new VP_getItems();
			return _instance;
		}
		
		public function get interfaceUI():Sprite
		{
			return _interfaceUI;
		}
	}
}